{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Number of Common Factors"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #enumeration #number-theory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #枚举 #数论"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: commonFactors"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #公因子的数目"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个正整数 <code>a</code> 和 <code>b</code> ，返回 <code>a</code> 和 <code>b</code> 的 <strong>公</strong> 因子的数目。</p>\n",
    "\n",
    "<p>如果 <code>x</code> 可以同时整除 <code>a</code> 和 <code>b</code> ，则认为 <code>x</code> 是 <code>a</code> 和 <code>b</code> 的一个 <strong>公因子</strong> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>a = 12, b = 6\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>12 和 6 的公因子是 1、2、3、6 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>a = 25, b = 30\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>25 和 30 的公因子是 1、5 。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= a, b &lt;= 1000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [number-of-common-factors](https://leetcode.cn/problems/number-of-common-factors/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [number-of-common-factors](https://leetcode.cn/problems/number-of-common-factors/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['12\\n6', '25\\n30']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        count =0\n",
    "        for i in range(1,1500):\n",
    "            if a %i ==0 and b%i == 0:\n",
    "                count+=1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        g = max(a,b)\n",
    "        list=[]\n",
    "        for i in range(1,g+1):\n",
    "            if a%i==0 and b%i==0:\n",
    "                list.append(i)\n",
    "        os = len(list)\n",
    "        return os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        if a == 0 or b == 0:\n",
    "            return 0\n",
    "        elif a == 1 or b == 1:\n",
    "            return 1\n",
    "        else:\n",
    "            count = 0\n",
    "            if a > b:  # a是较小数\n",
    "                tmp = a\n",
    "                a = b\n",
    "                b = tmp\n",
    "            for i in range(1, a+1):\n",
    "                if a % i == 0 and b % i == 0:\n",
    "                    count += 1\n",
    "            return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(1,a+1):\n",
    "            if a % i == 0 and b % i == 0:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        tmp = gcd = math.gcd(a, b)\n",
    "        f = []\n",
    "        for i in range(2, int(sqrt(gcd + 0.5)) + 1):\n",
    "            while tmp % i == 0:\n",
    "                f.append(i)\n",
    "                tmp //= i\n",
    "        if tmp > 1:\n",
    "            f.append(tmp)\n",
    "        cnt = Counter(f)\n",
    "        ans = 1\n",
    "        for k, v in cnt.items():\n",
    "            ans = ans * (v + 1)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a, b):\n",
    "        num =0\n",
    "        z = min(a,b)\n",
    "        for i in range(1,z+1):\n",
    "            if a % i == 0 and b % i == 0:\n",
    "                num +=1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(1, min(a, b) + 1):\n",
    "            if a % i == 0 and b % i == 0:\n",
    "                res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        if a > b:\n",
    "            a, b = b, a\n",
    "        res = 0\n",
    "        for t in range(1, a + 1):\n",
    "            if a % t == 0 and b % t == 0:\n",
    "                res = res + 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        g=gcd(a,b)\n",
    "        ans, i=0,1\n",
    "        while i*i<=g:\n",
    "            if g%i==0:\n",
    "                ans+=1\n",
    "                if i*i<g:\n",
    "                    ans+=1\n",
    "            i+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(1, min(a, b) + 1):\n",
    "            if a % i == 0 and b % i == 0: ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        k = a if a >= b else b\n",
    "        ans = 0\n",
    "        for i in range(1,k+1):\n",
    "            if a%i == 0 and b%i == 0:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        a_l, b_l=[a], [b]\n",
    "        for i in range(1, a):\n",
    "            if a%i == 0:\n",
    "                a_l.append(i)\n",
    "        for i in range(1, b):\n",
    "            if b%i == 0:\n",
    "                b_l.append(i)\n",
    "        if a%b == 0:\n",
    "            return len(b_l)\n",
    "        return len(a_l) + len(b_l) - len(set(a_l + b_l))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        g = gcd(a, b)\n",
    "        ans, i = 0, 1\n",
    "        while i * i <= g:\n",
    "            if g % i == 0:\n",
    "                ans += 1\n",
    "                if i * i < g:\n",
    "                    ans += 1\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        c, ans = gcd(a, b), 0\n",
    "        x = 1\n",
    "        while x * x <= c:\n",
    "            if c % x == 0:\n",
    "                ans += 1\n",
    "                if x * x != c:\n",
    "                    ans += 1\n",
    "            x += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        res = 0\n",
    "        if a < b:\n",
    "            ans = a\n",
    "        else:\n",
    "            ans = b\n",
    "        for i in range(1,ans+1):\n",
    "            if a % i == 0 and b % i == 0:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        g = gcd(a, b)\n",
    "        ans, i = 0, 1\n",
    "        while i * i <= g:\n",
    "            if g % i == 0:\n",
    "                ans += 1  # i 是公因子\n",
    "                if i * i < g:\n",
    "                    ans += 1  # g/i 是公因子\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        sum = 0\n",
    "        for i in range(1,min(a,b)+1):\n",
    "            if not(a%i or b%i):\n",
    "                sum += 1\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        s = min(a, b)\n",
    "        ans = 0\n",
    "        for i in range(1, s + 1): \n",
    "            if a % i == 0 and b % i == 0:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        m=min(a,b)\n",
    "        ans=0\n",
    "        for i in range(1,m+1):\n",
    "            if a%i==0 and b%i==0:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        res=0\n",
    "        for i in range(1,min(a,b)+1):\n",
    "            if a%i==0 and b%i==0:\n",
    "                res+=1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        res = 0\n",
    "        n = min(a, b)\n",
    "        for i in range(1, n+1):\n",
    "            if a % i == 0 and b % i == 0:\n",
    "                res += 1\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(1, min(a, b)+1):\n",
    "            if a % i == 0 and b % i == 0:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        mini=min(a,b)\n",
    "        x,i=0,1\n",
    "        while i<=mini:\n",
    "            if a%i==0 and b%i==0:\n",
    "                x+=1\n",
    "            i+=1\n",
    "        return x\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        ans = 0\n",
    "        for each in range(1,min(a,b)+1):\n",
    "            if a%each ==0 and b%each ==0:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        gcd = math.gcd(a, b)\n",
    "        return sum(1 for i in range(1, gcd + 1) if gcd % i == 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        cnt=0\n",
    "        for i in range(1,min(a,b)+1):\n",
    "            if a%i==0 and b%i==0:\n",
    "                cnt+=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        count = 0\n",
    "        a, b = min(a, b), max(a, b)\n",
    "        for i in range(1, a+1):\n",
    "            if not a%i and not b%i:\n",
    "                count += 1\n",
    "\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        count = 0\n",
    "        for i in range(1 , min(a , b)+1):\n",
    "            if a % i == 0 and b % i == 0:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        cnt = 0\n",
    "        min_num = min(a,b)\n",
    "        max_num = max(a,b)\n",
    "        for i in range(1,min_num//2+1):\n",
    "            if a % i == 0 and b % i == 0:\n",
    "                cnt += 1\n",
    "        if max_num % min_num == 0:\n",
    "            cnt +=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        # def getlist(n):\n",
    "        #     result = []\n",
    "        #     for i in range(1,n+1):\n",
    "        #         if n % i ==0:\n",
    "        #             result.append(i)\n",
    "        #     return set(result)\n",
    "        # return len(list(getlist(a) & getlist(b)))\n",
    "        return len(list(set([i for i in range(1,a+1) if a % i == 0]) & set([i for i in range(1,b+1) if b % i == 0])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        count = 0\n",
    "        for i in range(1,min(a,b)+1):\n",
    "            if a%i==0 and b%i==0:\n",
    "                count+=1\n",
    "        return count "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        return len([0 for i in range(1,max(a,b)+1) if not (a%i or b%i)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        res = 1\n",
    "        for i in range(2, min(a, b)+1):\n",
    "            if a%i == b%i == 0:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        def gcdAB(m, n):\n",
    "            if n == 0:\n",
    "                return m\n",
    "            return gcd(n, m % n)\n",
    "\n",
    "        c = gcdAB(a, b)\n",
    "        ans = 0\n",
    "        for i in range(1, c + 1):\n",
    "            if a % i == 0 and b % i == 0:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        if a < b:\n",
    "            a, b = b, a\n",
    "        g = self.gcd(a, b)\n",
    "        fset = set([g])\n",
    "        for i in range(1, g//2+1):\n",
    "            if g % i == 0:\n",
    "                fset.add(i)\n",
    "                fset.add(g//i)\n",
    "        return len(fset)\n",
    "\n",
    "    def gcd(slef, m, n):\n",
    "        while n != 0:\n",
    "            m, n = n, m % n\n",
    "        return m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        return sum([1 for i in range(1,min(a,b)+1) if a%i==b%i==0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        g = gcd(a, b)\n",
    "        ans, i = 0, 1\n",
    "        while i * i <= g:\n",
    "            if g % i == 0:\n",
    "                ans += 1  # i 是公因子\n",
    "                if i * i < g:\n",
    "                    ans += 1  # g/i 是公因子\n",
    "            i += 1\n",
    "        return ans\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        count = 0\n",
    "        for i in range(1,min(a,b)+1):\n",
    "            if a%i==0 and b%i==0:\n",
    "                count += 1\n",
    "        return count \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def commonFactors(self, a: int, b: int) -> int:\n",
    "#         mi=min(a,b)\n",
    "#         ans=0\n",
    "#         for i in range(1,mi+1):\n",
    "#             if a%i==0 and b%i==0:\n",
    "#                 ans+=1\n",
    "#         return ans\n",
    "\n",
    "# ### 官2：枚举到最大公约数\n",
    "# class Solution:\n",
    "#     def commonFactors(self, a: int, b: int) -> int:\n",
    "#         c, ans = gcd(a, b), 0\n",
    "#         x = 1\n",
    "#         while x * x <= c:\n",
    "#             if c % x == 0:\n",
    "#                 ans += 1\n",
    "#                 if x * x != c:\n",
    "#                     ans += 1\n",
    "#             x += 1\n",
    "#         return ans\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self,a,b):\n",
    "        c,ans=gcd(a,b),0\n",
    "        i=1\n",
    "        while i*i<=c:\n",
    "            if c%i==0:\n",
    "                ans+=1\n",
    "                if i*i<c:\n",
    "                    ans+=1\n",
    "            i+=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        count=0\n",
    "       \n",
    "        for i in range(1,a+1):\n",
    "            if a%i==0 and b%i==0:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        cnt = 0\n",
    "        minn = min(a,b)+1\n",
    "        for i in range(1,minn):\n",
    "            if a%i == 0 and b%i == 0:\n",
    "                cnt+=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        g=gcd(a,b)\n",
    "        ans=0\n",
    "        x=1\n",
    "        while x<=g:\n",
    "            if g%x==0:\n",
    "                ans+=1\n",
    "            x+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        ans = []\n",
    "        y = min(a,b)\n",
    "        for x in range(1, y+1):\n",
    "            if a % x == 0 and b % x ==0:\n",
    "                ans.append(x)\n",
    "        return len(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(1,min(a,b)+1):\n",
    "            if a % i == 0 and b % i == 0:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(1,a + 1 if a < b else b + 1):\n",
    "            if a % i == 0 and b % i ==0:\n",
    "                res +=1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        mini=min(a,b)\n",
    "        x=0\n",
    "        i=1\n",
    "        while i<=mini:\n",
    "            if a%i==0 and b%i==0:\n",
    "                x+=1\n",
    "            i+=1\n",
    "        return x\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        factor_a = []\n",
    "        factor_b = []\n",
    "        \n",
    "        commonfactors = 0\n",
    "        for i in range(1,a+1):\n",
    "            if(a % i == 0):\n",
    "                factor_a.append(i)\n",
    "        for i in range(1,b+1):\n",
    "            if(b % i == 0):\n",
    "                factor_b.append(i)\n",
    "        \n",
    "        for i in range(len(factor_a)):\n",
    "            for j in range(len(factor_b)):\n",
    "                if(factor_a[i]==factor_b[j]):\n",
    "                    commonfactors = commonfactors + 1\n",
    "        return commonfactors\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        fac = int((min(a,b)))\n",
    "        return sum(1 for i in range(1,fac+1) if (a%i==0 and b%i==0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(1,min(a,b)+1):\n",
    "            if a%i==0 and b%i==0:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:                                   \n",
    "                                   \n",
    "                count=0\n",
    "                if a>b:\n",
    "                    for i in range(1,a+1):\n",
    "                        if i != 0 and a%i==0 and b%i==0:\n",
    "                            \n",
    "                            count+=1\n",
    "                else:\n",
    "                    for i in range(1,b+1):\n",
    "                        if i != 0 and a%i==0 and b%i==0:\n",
    "                            \n",
    "                            count+=1\n",
    "\n",
    "                return count \n",
    "                                            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        n = min(a, b)\n",
    "        ans = 0\n",
    "        for i in range(1, n + 1):\n",
    "            if a % i == 0 and b % i == 0:\n",
    "                ans += 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        res = reduce(math.gcd, [a, b])\n",
    "        if res == 1:\n",
    "            return 1\n",
    "        total = 2\n",
    "        for i in range(res-1, 1, -1):\n",
    "            if a % i == 0 and b % i == 0:\n",
    "                total += 1\n",
    "        return total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        aa=[]\n",
    "        bb=[]\n",
    "        for i in range(1,a+1):\n",
    "            for o in range(1,a+1):\n",
    "                if i*o==a:\n",
    "                    aa.append(i)\n",
    "        for i in range(1,b+1):\n",
    "            for o in range(1,b+1):\n",
    "                if i*o==b:\n",
    "                    bb.append(i)\n",
    "        print(bb)\n",
    "        return len([x for x in set(aa)if x in set(bb)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        ans=0\n",
    "        def gcd(a, b):\n",
    "            while b != 0:\n",
    "                temp = a % b\n",
    "                a = b\n",
    "                b = temp\n",
    "            return a\n",
    "        c=gcd(a,b)\n",
    "        for i in range(1,c+1):\n",
    "            if c%i==0:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(1,a + 1 if a < b else b + 1):\n",
    "            if a % i == 0 and b % i ==0:\n",
    "                res +=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        res=0\n",
    "        for i in range(1,min(a,b)+1):\n",
    "            if a%i==0 and b%i==0:\n",
    "                res+=1\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        result = 0\n",
    "        for i in range(1,min(a,b)+1):\n",
    "            if a % i == 0 and b % i == 0:\n",
    "                result += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        all_list = []\n",
    "        res = []\n",
    "        for i in range(1,a+1):\n",
    "            if i not in all_list:\n",
    "                all_list.append(i)\n",
    "        for j in range(1,b+1):\n",
    "            if j not in all_list:\n",
    "                all_list.append(j)\n",
    "        for s in all_list:\n",
    "            c1 = a % s\n",
    "            c2 = b % s\n",
    "            if c1==0 and c2==0:\n",
    "                res.append(s)\n",
    "        return len(res)\n",
    "\n",
    "         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        ans=0\n",
    "        for i in range(1,min(a,b)+1):\n",
    "            if a%i==0 and b%i==0:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        count=0\n",
    "        for i in range(1,min(a,b)+1):\n",
    "            if a%i==0 and b%i==0:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def commonFactors(self, a: int, b: int) -> int:\n",
    "    ans = 1\n",
    "    for i in range(2,min(a,b)+1):\n",
    "      if a%i ==0 and b%i==0: ans +=1\n",
    "    return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        res=0\n",
    "        for i in range(1,min(a,b)+1):\n",
    "            if a%i==0 and b%i==0:\n",
    "                res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(1,min(a,b)+1):\n",
    "            if a%i == 0 and b%i == 0:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "\n",
    "        result = 0\n",
    "\n",
    "        for i in range(1, min([a, b])+1):\n",
    "            if (a % i == 0 and b % i == 0):\n",
    "                result += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        count = 1\n",
    "        for i in range(2,gcd(a,b)+1):\n",
    "            if a%i==0 and b%i==0:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        ans=0\n",
    "        for x in range (1,min(a,b)+1):\n",
    "            if a%x==0 and b%x==0:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        mi=min(a,b)\n",
    "        ans=0\n",
    "        for i in range(1,mi+1):\n",
    "            if a%i==0 and b%i==0:\n",
    "                ans+=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def commonFactors(self, a: int, b: int) -> int:\n",
    "        n = 0\n",
    "        if a >= b :\n",
    "            for i in range(1,b+1):\n",
    "                if a%i + b%i == 0:\n",
    "                    n+=1\n",
    "        else:\n",
    "            for i in range(1,a+1):\n",
    "                if a%i + b%i ==0:\n",
    "                    n+=1\n",
    "\n",
    "        return n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
